const debug = process.env.NODE_ENV == 'development' ? true : false

const bgm = uni.createInnerAudioContext();
bgm.src = ''
bgm.loop = true;

const state = () => ({
	mute: false
})

const mutations = {
	// 播放背景音乐
	setPlayBgMusic(state, mute) {
		console.log('设置是否静音', mute);
		state.mute = mute
	}

}
const actions = {
	playBgMusic({ state, commit, dispatch }) {
		console.log('bgm===>',bgm)
		// 是否创建音乐实例
		if (!bgm) return;
		if (state.mute) {
			console.log('bgm/playBgMusic', 'mute静音');
			bgm.pause()
		} else {
			console.log('默认开启游戏背景音乐');
			bgm.play()
		}
		bgm.onPause(() => {
			console.log('暂停背景音乐');
		})
		bgm.onPlay(() => {
			console.log('开始播放音乐#######');
		})
		bgm.onError((error) => {
			console.log('error',error)
		})
	},
	des({ state, commit, dispatch }) {
		// bgm.src = ''
		bgm.stop()
		bgm.destroy()
		console.log('销毁背景音乐实例');
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}